package com.example.woke;

import java.util.List;
import org.apache.http.Header;
import org.apache.http.cookie.Cookie;
import org.json.JSONException;
import org.json.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.method.MyApp;
import com.woke.serizedatas.Datas_load;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class shop extends Activity {



	private SharedPreferences sharep;
	private MyApp application;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		
		sharep = getSharedPreferences("loaduser", Context.MODE_PRIVATE);
		application = (MyApp) getApplication();
		
//		intview();
		loadphone();
		
	}
		/**  
	    * ��ȡ��׼ Cookie   
	    */  
	   private List<Cookie> getCookieText() {  
	       PersistentCookieStore myCookieStore = new PersistentCookieStore(shop.this);  
	       List<Cookie> cookies = myCookieStore.getCookies();  
//	       Log.e("cookies.size() = " ,"  "+ cookies.size());  
//	       Log.e("cookies.size() = " ,"  "+ cookies);  
	       return cookies;  
	   }  



	private void loadphone() {
		AsyncHttpClient ahc = new AsyncHttpClient();
        PersistentCookieStore myCookieStore = new PersistentCookieStore(shop.this);    
        ahc.setCookieStore(myCookieStore);
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "Scancode");
		params.put("a", "getMerchant");
		params.put("userId", "1391");
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("登录dd数据", ""+response);
				getCookieText();
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if(flag.equals("success")){
						String jsonObject = response.getString("data");
						String sh = sharep.getString("data", jsonObject);
						System.err.println("sh"+sh);
						Datas_load datas_load = JSONArray.parseObject(sh, Datas_load.class);
						application.setMerchantsType(datas_load);
						Editor edit = sharep.edit();
						System.out.println(edit);
						edit.putString("load", "load");
						edit.commit();
						
						
					}else{
						
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onStart() {
				super.onStart();
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Toast.makeText(shop.this, "登录失败！服务器未响应", Toast.LENGTH_SHORT).show();
				Log.e("response", "失败"+responseString);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
        		Log.e("d", "d");
			}
		});
	}
	
}
